<template>
  <view class="cart" :style="{'min-height':windowHerght+'px'}" v-if="cart.length!==0">

    <my-address></my-address>
    <view class="cart_title">
      <uni-icons type="shop" size="24"></uni-icons>
      <text>购物车</text>
    </view>

    <uni-swipe-action>
      <block v-for="(goods,index) in cart" :key="index">
        <uni-swipe-action-item :right-options="options" @click="swipeActionClickHandler(goods)" :autoClose="true">
          <my-goods :show-num="true" :goods="goods" :show-radio="true" @radio-change="radioChangeHandler"
            @num-change="numChangeHandler"></my-goods>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>
    <my-settle></my-settle>
  </view>

  <view class="empty-cart" :style="{height:windowHerght+50+'px'}" v-else>
    <image src="/static/cart_empty@2x.png" class="empty-image" mode="aspectFill"></image>
    <text class="tip-text">空空如也</text>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  import {
    mapState,
    mapMutations
  } from 'vuex'
  export default {
    mixins: [badgeMix],
    computed: {
      ...mapState('m_cart', ['cart'])
    },
    data() {
      return {
        windowHerght: 0,
        options: [{
          text: '删除', // 显示的文本内容
          style: {
            backgroundColor: '#C00000' // 按钮的背景颜色
          }
        }]
      }
    },
    methods: {
      ...mapMutations('m_cart', ['updateGoodsState', 'updateGoodsCount', 'removeGoodsById']),
      radioChangeHandler(e) {
        // e = {goods_id,goods_state}
        this.updateGoodsState(e)
      },
      numChangeHandler(e) {
        // e = {goods_id,goods_count}
        this.updateGoodsCount(e)
      },
      swipeActionClickHandler(goods) {
        this.removeGoodsById(goods.goods_id)
      }
    },
    onLoad() {
      const info = uni.getSystemInfoSync()
      this.windowHerght = info.windowHeight - 50
    }
  }
</script>

<style lang="scss">
  .cart {
    background-color: #fff;
    width: 100vw;
    padding-bottom: 50px;

    .cart_title {
      height: 80rpx;
      display: flex;
      align-items: center;
      border-bottom: 5rpx solid #efefef;
      padding-left: 10rpx;

      text {
        margin-left: 20rpx;
        font-size: 30rpx;
        vertical-align: middle;
      }
    }
  }

  .empty-cart {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    .empty-image {
      width: 90px;
      height: 90px;
    }

    .tip-text {
      margin-top: 15px;
      font-size: 28rpx;
      color: gray;
    }
  }
</style>
